#Copyright Ken Faulkner 2007.
import sys
import wx
from Common.logRoutine import *
import traceback
from Framework.GUI.MainPanel import MainPanel
import os
import os.path
import shutil

class BBCMainFrame( wx.Frame ):
  """
  Main frame for the UI.
  Still being tweaked.
  """
  def __init__(self, parent, ID, title, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):

    wx.Frame.__init__(self, parent, ID, title, pos, size, style)

    MenuBar = wx.MenuBar()

    HelpMenu = wx.Menu()

    item = HelpMenu.Append(wx.ID_HELP, "Test &Help",
                            "Help for this simple test")
    self.Bind(wx.EVT_MENU, self.on_about_request, item)

    ## this gets put in the App menu on OS-X
    item = HelpMenu.Append(wx.ID_ABOUT, "&About",
                            "More information About this program")
    self.Bind(wx.EVT_MENU, self.on_about_request, item)
    MenuBar.Append(HelpMenu, "&Help")
    self.SetMenuBar(MenuBar)


  def on_about_request( self, foo ):
    """
    Write something :)
    """
    messageDialog = wx.MessageDialog( self, "A little BBC proggy in need of some work", "About BBCPlayer", wx.OK | wx.ICON_INFORMATION)
    messageDialog.ShowModal()
    messageDialog.Destroy()




class MainApp(wx.App):



  def generate_config_files( self ):
    """
    Probably shouldn't be here, but needs to be really early in the startup.
    """    
  
    # various config files.
    # self.config for global config.
    # self.userConfig for user specific info.
    home_dir = os.path.join( os.getenv("HOME"), ".bbc") 
    
    self.log.debug("home dir " + home_dir )
    
    if not os.path.isdir( home_dir ):
      os.mkdir( home_dir )
      os.mkdir( os.path.join( home_dir, "cache") )
      
      bbc = os.path.join( home_dir, "bbc.cfg") 
      
      self.log.debug("bbc " + bbc )
      
      shutil.copyfile("Config/bbc.cfg", bbc )
      
      user = os.path.join( home_dir, "user.cfg") 
      
      self.log.debug("user " + user )
      
      shutil.copyfile("Config/user.cfg", user )
          

  def OnInit(self):

    self.log = getLogger()

    try:
    
      self.generate_config_files()
      
      wx.InitAllImageHandlers()

      self.SetAppName("foooo")
      self.SetClassName("bbaaarr")

      # need to get the sizing from the config file.
      frame = BBCMainFrame( None, -1, "BBBC", size=(1000,600)) 

      bm = wx.Bitmap("images/icon.png", wx.BITMAP_TYPE_PNG)
      icon = wx.IconFromBitmap( bm )
      frame.SetIcon( icon )

      p = MainPanel( frame  )
  
      self.SetTopWindow(frame)
      frame.Show()
      
    except:
      self.log.error("MainApp ex " + traceback.format_exc() )


    return 1


#----------------------------------------------------------------------


if __name__ == '__main__':

  app = MainApp(0)
  app.MainLoop()
